﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_CD
{
    public partial class BaoCaoDanhSachChuTichCongDoanCoSo_Update : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;

            CheckPermission();
            if (!IsPostBack)
            {
                Config();
                BindDataToControls();
            }
        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("DanhSachChuTichCongDoanCoSo");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Thủ tục cấu hình giao diện, các controls
        /// </summary>
        private void Config()
        {
            //TODO
            txtNguoiLapBaoCao.Text = "";
            txtThuTruongCoQuanChinhTri.Text = "";
        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {

                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];

                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);


            }


        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM AND IsCommon <>1";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                btnLuu.CommandName = MyConst.Action.Addnew;
                DataRow dr = dt.NewRow();
                dr["F2"] = "";
                dr["F3"] = "";
                dr["F4"] = "";
                dr["F5"] = "";
                dr["F6"] = "";
                dr["F7"] = "";
                dr["F8"] = "";
                dr["F9"] = "";
                dr["F10"] = "";
                dr["F11"] = "";
                dr["F12"] = "";
                dr["F13"] = "";
                dr["F14"] = 0;
                dr["F15"] = 0;
                dr["F16"] = 0;
                dr["F17"] = 0;
                dr["F18"] = "";
                dr["F19"] = "";
                dr["F20"] = 0;
                dt.Rows.InsertAt(dr, 0);

            }
            else
            {
                BindDataToTextField();
                btnLuu.CommandName = MyConst.Action.Update;
            }
            grvData.DataSource = dt;
            grvData.DataBind();
            if (IsEmpty)
            {
                grvData.Rows[0].Visible = false;
            }
        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT * FROM dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtTenBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtNguoiLapBaoCao.Text = dt.Rows[0]["CM_NguoiTongHop"].ToString();
                txtThuTruongCoQuanChinhTri.Text = dt.Rows[0]["CM_ThuTruong"].ToString();
            }
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = true;
                grvData.Visible = true;
                grvData.EditIndex = -1;
                BindDataToGrid();
            }
            else
            {
                btnLuu.Visible = false;
                grvData.Visible = false;
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count == 0)
            {
                btnLuu.CommandName = MyConst.Action.Addnew;
            }
            else
            {
                btnLuu.CommandName = MyConst.Action.Update;
            }

            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                string sSQL = "INSERT INTO dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy" +
                              "(KyBaoCaoID_DM ,DonViID ,CM_NguoiTongHop ,CM_ThuTruong ,CM_TenBaoCao ,CM_ThoiGianBaoCao" +
                              ",IsCommon ,CreateDate ,UpdateDate ,UserID)" +
                              "VALUES  ( @KyBaoCaoID_DM ,@DonViID ,@CM_NguoiTongHop ,@CM_ThuTruong ,@CM_TenBaoCao ," +
                              "GETDATE() ,1 ,GETDATE() ,GETDATE() ,@UserID)";

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiTongHop", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"])));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
            else
            {
                string sSQL = "Update tblDanhSachChuTichCongDoanCoSoTheoNhiemKy Set CM_NguoiTongHop=@CM_NguoiTongHop, CM_ThuTruong=@CM_ThuTruong, CM_TenBaoCao=@CM_TenBaoCao " +
                                "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiTongHop", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));


                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
        }

        #endregion Events


        #region GridCommand

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "STT");


                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Họ và tên Quê quán Trú quán");

                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Năm sinh");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "N.ngũ T.dụng");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Cấp bậc (SQ,QNCN,CNV...)");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Trình độ");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Chúc vụ");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Qua đào tạo Công đoàn");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Nam, Nữ, Dân tộc");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Chuyên trách");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Được bầu cử chủ tịch lần 1");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Tái cử lần...");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TSố cán bộ BCH");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TSố ĐVCĐCS");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "-Tên CĐCS -Loại hình CĐCS");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Tổ chức CĐCS(1,2,3,4 cấp...)");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Tổ chức CĐCS có UBKT");

                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);

                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "C.môn (Qua trường)");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "C.trị (SC,TC,Cao cấp)");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Đảng");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chính quyền");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "ĐH C.đoàn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Bồi dưỡng N.vụ");

                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "16");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "17");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "18");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "19");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "20");

                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_CD.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtF2Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF2Edit");
                    TextBox txtF3Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF3Edit");
                    TextBox txtF4Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF4Edit");
                    TextBox txtF5Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF5Edit");
                    TextBox txtF6Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF6Edit");
                    TextBox txtF7Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF7Edit");
                    TextBox txtF8Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF8Edit");
                    TextBox txtF9Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF9Edit");
                    TextBox txtF10Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF10Edit");
                    TextBox txtF11Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF11Edit");
                    TextBox txtF12Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF12Edit");
                    TextBox txtF13Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF13Edit");
                    TextBox txtF14Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF14Edit");
                    TextBox txtF15Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF15Edit");
                    TextBox txtF16Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF16Edit");
                    TextBox txtF17Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF17Edit");
                    TextBox txtF18Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF18Edit");
                    TextBox txtF19Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF19Edit");
                    TextBox txtF20Edit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtF20Edit");

                    string sSQL = "UPDATE dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy " +
                                  "SET F2 = @F2,F3 = @F3,F4 = @F4,F5 = @F5,F6 = @F6,F7 = @F7,F8 = @F8,F9 = @F9," +
                                  "F10 = @F10,F11 = @F11,F12 = @F12,F13 = @F13,F14 = @F14,F15 = @F15,F16 = @F16," +
                                  "F17 = @F17,F18 = @F18,F19 = @F19,F20 = @F20,UpdateDate = GETDATE(),UserID = @UserID " +
                                  "WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.Float, ParameterDirection.Input, txtF14Edit.Text.Trim().Equals("") ? "0" : txtF14Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.Float, ParameterDirection.Input, txtF15Edit.Text.Trim().Equals("") ? "0" : txtF15Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F16", SqlDbType.Float, ParameterDirection.Input, txtF16Edit.Text.Trim().Equals("") ? "0" : txtF16Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F17", SqlDbType.Float, ParameterDirection.Input, txtF17Edit.Text.Trim().Equals("") ? "0" : txtF17Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F18", SqlDbType.NVarChar, ParameterDirection.Input, txtF18Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F19", SqlDbType.NVarChar, ParameterDirection.Input, txtF19Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F20", SqlDbType.Float, ParameterDirection.Input, txtF20Edit.Text.Trim().Equals("") ? "0" : txtF20Edit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));

                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData.EditIndex = -1;
                    BindDataToGrid();
                    MyCommon.AddMessage(this.Page.Master, "Cập nhật thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Page.Master, "Có lỗi trong dữ liệu nhập vào, một số trường năm sinh, Được bầu cử chủ tịch lần 1, Tái cử lần... bắt buộc phải là một con số !", MyConst.MessageType.Warning);
                    MyTrans.Rollback();
                }

            }
            else if (e.CommandName == AIC_CD.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtF2New = (TextBox)grvData.FooterRow.FindControl("txtF2New");
                    TextBox txtF3New = (TextBox)grvData.FooterRow.FindControl("txtF3New");
                    TextBox txtF4New = (TextBox)grvData.FooterRow.FindControl("txtF4New");
                    TextBox txtF5New = (TextBox)grvData.FooterRow.FindControl("txtF5New");
                    TextBox txtF6New = (TextBox)grvData.FooterRow.FindControl("txtF6New");
                    TextBox txtF7New = (TextBox)grvData.FooterRow.FindControl("txtF7New");
                    TextBox txtF8New = (TextBox)grvData.FooterRow.FindControl("txtF8New");
                    TextBox txtF9New = (TextBox)grvData.FooterRow.FindControl("txtF9New");
                    TextBox txtF10New = (TextBox)grvData.FooterRow.FindControl("txtF10New");
                    TextBox txtF11New = (TextBox)grvData.FooterRow.FindControl("txtF11New");
                    TextBox txtF12New = (TextBox)grvData.FooterRow.FindControl("txtF12New");
                    TextBox txtF13New = (TextBox)grvData.FooterRow.FindControl("txtF13New");
                    TextBox txtF14New = (TextBox)grvData.FooterRow.FindControl("txtF14New");
                    TextBox txtF15New = (TextBox)grvData.FooterRow.FindControl("txtF15New");
                    TextBox txtF16New = (TextBox)grvData.FooterRow.FindControl("txtF16New");
                    TextBox txtF17New = (TextBox)grvData.FooterRow.FindControl("txtF17New");
                    TextBox txtF18New = (TextBox)grvData.FooterRow.FindControl("txtF18New");
                    TextBox txtF19New = (TextBox)grvData.FooterRow.FindControl("txtF19New");
                    TextBox txtF20New = (TextBox)grvData.FooterRow.FindControl("txtF20New");

                    string sql = "INSERT INTO dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy " +
                                 "(KyBaoCaoID_DM ,DonViID ,F2 ,F3 ,F4 ,F5 ,F6 ,F7 ,F8 ,F9 ,F10 ,F11 ,F12 ,F13 ,F14 ," +
                                 "F15 ,F16 ,F17 ,F18 ,F19 ,F20 ,CreateDate ,UpdateDate ,UserID,IsCommon)" +
                                 "VALUES  ( @KyBaoCaoID_DM,@DonViID,@F2,@F3,@F4,@F5,@F6,@F7,@F8,@F9,@F10,@F11,@F12," +
                                 "@F13,@F14,@F15,@F16,@F17,@F18,@F19,@F20,GETDATE(),GETDATE(),@UserID,0)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtF2New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtF3New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtF4New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtF5New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtF6New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtF7New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtF8New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtF9New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtF10New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtF11New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtF12New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtF13New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.Float, ParameterDirection.Input, txtF14New.Text.Trim().Equals("") ? "0" : txtF14New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.Float, ParameterDirection.Input, txtF15New.Text.Trim().Equals("") ? "0" : txtF15New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F16", SqlDbType.Float, ParameterDirection.Input, txtF16New.Text.Trim().Equals("") ? "0" : txtF16New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F17", SqlDbType.Float, ParameterDirection.Input, txtF17New.Text.Trim().Equals("") ? "0" : txtF17New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F18", SqlDbType.NVarChar, ParameterDirection.Input, txtF18New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F19", SqlDbType.NVarChar, ParameterDirection.Input, txtF19New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F20", SqlDbType.Float, ParameterDirection.Input, txtF20New.Text.Trim().Equals("") ? "0" : txtF20New.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        MyCommon.AddMessage(this.Page.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        MyCommon.AddMessage(this.Page.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid();
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Page.Master, "Có lỗi trong dữ liệu nhập vào, một số trường năm sinh, Được bầu cử chủ tịch lần 1, Tái cử lần... bắt buộc phải là một con số !", MyConst.MessageType.Warning);
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_CD.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData.EditIndex = -1;
                BindDataToGrid();
                MyCommon.AddMessage(this.Page.Master, "Xóa thành công", MyConst.MessageType.Success);
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }



        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }
    }
}
